using System;
using System.Collections.Generic;
using System.Linq;
using BLToolkit.Data;
using BLToolkit.Data.Linq;
using BLToolkit.DataAccess;

namespace dbvc.Queries
{
    public class GenericQuery<T> where T : class
    {
        public static T Get(int project)
        {
            using (var db = new DAA())
            {
                var query = new SqlQuery<T>(db);
                return query.SelectByKey(project);
            }
        }

        public static IList<T> GetAll()
        {
            using (var db = new DAA())
            {
                var query = new SqlQuery<T>(db);
                return query.SelectAll();
            }
        }

        public static void Create(T obj)
        {
            using (var db = new DAA())
            {
                var query = new SqlQuery<T>(db);
                query.Insert(obj);
            }
        }

        public static void Create(T obj, Action<T, int> setId)
        {
            using (var db = new DAA())
            {
                var id = db.InsertWithIdentity(obj);
                setId(obj, Convert.ToInt32(id));
            }
        }

        public static void Delete(T obj)
        {
            using (var db = new DAA())
            {
                var query = new SqlQuery<T>(db);
                query.Delete(obj);
            }
        }

        public static void Delete(IEnumerable<T> objs)
        {
            using (var db = new DAA())
            {
                var query = new SqlQuery<T>(db);
                query.Delete(objs);
            }
        }

        public static int Count()
        {
            using (var db = new DAA())
            {
                return db.GetTable<T>().Count();
            }
        }

        public static void DeleteAll()
        {
            Delete(GetAll());
        }
    }
}